package com.more.spring.boot.springmybatisdemo.controller;

import com.more.spring.boot.springmybatisdemo.pojo.User;
import com.more.spring.boot.springmybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.stream.Collectors;

@Controller
//@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    @ResponseBody
    public String hello() {
        return "hello";
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @RequestMapping(value = "/user/queryUserList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<User> queryUserList() {
        List<User> users = userService.queryUsers();
        System.out.println("执行结果2：");
        for (User user : users) {
            System.out.println(user);
        }
        return users;
    }


    /**
     * 根据用户名和性别查询用户信息
     *
     * @param uname
     * @param gender
     * @return
     */
    @RequestMapping(value = "/user/queryUserByNameAndGender", produces = {"application/json; charset=UTF-8"})
    @ResponseBody
    public List<User> queryUserByNameAndGender(String uname, String gender) {
        List<User> users = userService.queryUsers();
        System.out.println("执行结果3：");
        for (User user : users) {
            System.out.println(user);
        }
        return users.stream().filter(a -> a.getUname().equals(uname) && a.getGender().equals(gender)).collect(Collectors.toList());
    }
}
